package com.iqiyi.pps.epg.core.utils;

import com.iqiyi.pps.epg.core.model.stream.LiveChannelStream;

import java.util.*;

/**
 * Created by lemuel on 2015/9/16.
 */
public class StreamOrderComparator implements Comparator<LiveChannelStream> {
    public static final Boolean ASC = true;
    public static final Boolean DESC = false;
    private Boolean _order = ASC;
    private Map<String, Integer> _streamTypes = new HashMap<>();

    private StreamOrderComparator(){
    }

    public StreamOrderComparator(Map<String, Integer> streamTypes){
        _streamTypes = streamTypes;
    }

    public StreamOrderComparator(Map<String, Integer> streamTypes, Boolean order){
        _streamTypes = streamTypes;
        _order = order;
    }

    @Override
    public int compare(LiveChannelStream o1, LiveChannelStream o2) {
        Integer key1 = nullToZero(_streamTypes.get(o1.getStreamType()));
        Integer key2 = nullToZero(_streamTypes.get(o2.getStreamType()));

        if(_order == DESC) {
            return key2.compareTo(key1);
        }else{
            return key1.compareTo(key2);
        }
    }

    private static Integer nullToZero(Object obj){
        return obj == null ? Integer.valueOf(0) : Integer.valueOf(obj.toString());
    }

//    public static void main(String[] args) {
//        List<LiveChannelStream> list = new ArrayList<>();
//        Map<String, Integer> streamTypes = new HashMap<>();
//
//        List<String> types = new ArrayList<String>(){{add("HIGH_DEFINITION"); add("SMOOTH"); add("RESOLUTION_1080P");}};
//        List<Integer> vals = new ArrayList<Integer>(){{add(new Integer(3)); add(new Integer(2)); add(new Integer(6));}};
//
//        for(int i = 0; i< types.size(); ++i){
//            LiveChannelStream liveChannelStream = new LiveChannelStream();
//            liveChannelStream.setStreamType(types.get(i));
//            list.add(liveChannelStream);
//
//            streamTypes.put(types.get(i), vals.get(i));
//        }
//
//        Collections.sort(list, new StreamOrderComparator(streamTypes, StreamOrderComparator.DESC));
//
//        System.out.println(list);
//
//    }
}
